package com.tnl.hunter.webchat.utils;

import com.alibaba.fastjson.JSONObject;
import com.biz.system.config.Config;
import com.biz.system.config.WebchatServiceConstant;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.wxaapp.WxaConfig;
import com.jfinal.wxaapp.WxaConfigKit;
import com.jfinal.wxaapp.api.WxaAccessToken;
import com.jfinal.wxaapp.api.WxaAccessTokenApi;

public class WxaTemplateUtil {
	    /**
	     * 职道-小程序
	     * @param template
	     * @return
	     */
		public static boolean sendTemplateYRCMsg(String template){
			WxaConfigKit.setWxaConfig(getApiConfigZhiDao());
			String token=WxaAccessTokenApi.getAccessTokenStr();
	       boolean flag=false;   
	        String requestUrl="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN";  
	        requestUrl=requestUrl.replace("ACCESS_TOKEN", token);  
	        JSONObject jsonResult=CommonUtil.httpsRequest(requestUrl, "POST", template);  
	        if(jsonResult!=null){  
	            int errorCode=jsonResult.getInteger("errcode");  
	            String errorMessage=jsonResult.getString("errmsg");  
	            if(errorCode==0){  
	                flag=true;  
	            }else{  
	                System.out.println("模板消息发送失败:"+errorCode+","+errorMessage);  
	                flag=false;  
	            }  
	        }
	        return flag;   
		}
	    public static WxaConfig  getApiConfigZhiDao() {
			WxaConfig ac = new WxaConfig();

			// 配置微信 API 相关常量
			ac.setToken(WebchatServiceConstant.TOKEN);
			ac.setAppId(WebchatServiceConstant.APPID_ZHIDAO);
			ac.setAppSecret(WebchatServiceConstant.APPSECRET_ZHIDAO);
			ac.setEncodingAesKey(WebchatServiceConstant.EncodingAESKey_ZHIDAO);
			return ac;
		}
	    
	    /**
	     * 跳哪里服务号
	     * @return
	     */
	     private static String sendApiUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
	     public static ApiResult sendtiaonalis(String jsonStr) {
			 ApiConfigKit.putApiConfig(Config.getApiConfigTiaonaliS());
			 ApiConfigKit.setThreadLocalAppId(WebchatServiceConstant.APPID_TIAONALIS);
			 String token=AccessTokenApi.getAccessTokenStr();
			 String jsonResult = HttpUtils.post(sendApiUrl + token, jsonStr);
			 ApiConfigKit.removeThreadLocalAppId();
			 return new ApiResult(jsonResult);
		 }
	
	
}
